package com.wfh.aigenerator.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.wfh.aigenerator.annotation.AuthCheck;
import com.wfh.aigenerator.common.BaseResponse;
import com.wfh.aigenerator.common.ErrorCode;
import com.wfh.aigenerator.common.ResultUtils;
import com.wfh.aigenerator.constant.UserConstant;
import com.wfh.aigenerator.exception.BusinessException;
import com.wfh.aigenerator.model.dto.*;
import com.wfh.aigenerator.model.entity.User;
import com.wfh.aigenerator.model.vo.UserVO;
import com.wfh.aigenerator.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

/**
 * 用户表 控制层。
 *
 * @author wangfenghuan
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 保存用户表。
     *
     * @param userAddRequest 用户表
     * @return {@code true} 保存成功，{@code false} 保存失败
     */
    @PostMapping("/save")
    @Operation(summary = "增加用户")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> save(@RequestBody UserAddRequest userAddRequest) {
        String userAccount = userAddRequest.getUserAccount();
        String userPassword = userAddRequest.getUserPassword();
        String userName = userAddRequest.getUserName();
        String userRole = userAddRequest.getUserRole();
        if (StrUtil.hasBlank(userAccount, userPassword, userName, userRole)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User user = BeanUtil.copyProperties(userAddRequest, User.class);
        boolean save = userService.save(user);
        if (!save){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        return ResultUtils.success(user.getId());
    }

    /**
     * 用户注册
     *
     * @param request
     * @return
     */
    @PostMapping("/register")
    @Operation(summary = "用户注册")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest request) {
        if (request == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long l = userService.userRegister(request);
        return ResultUtils.success(l);
    }

    /**
     * 用户登录
     * @param userLoginRequest
     * @param request
     * @return
     */
    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public BaseResponse<UserVO> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request){
        if (userLoginRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserVO userVO = userService.userLogin(userLoginRequest, request);
        return ResultUtils.success(userVO);
    }

    /**
     * 用户退出登录
     * @param request
     * @return
     */
    @GetMapping("/logout")
    @Operation(summary = "用户退出登录")
    public BaseResponse<Boolean> userLogout(HttpServletRequest request){
        Boolean aBoolean = userService.userLogout(request);
        return ResultUtils.success(aBoolean);
    }

    /**
     * 获取当前登录用户
     *
     * @param request
     * @return
     */
    @GetMapping("/currentUser")
    @Operation(summary = "获取当前登录用户信息")
    public BaseResponse<UserVO> getLoginUser(HttpServletRequest request){
        User loginUser = userService.getLoginUser(request);
        UserVO userVO = BeanUtil.copyProperties(loginUser, UserVO.class);
        return ResultUtils.success(userVO);
    }

    /**
     * 根据主键删除用户表。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @PostMapping("/remove")
    @Operation(summary = "删除用户")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> remove(Long id) {
        if (id == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = userService.removeById(id);
        return ResultUtils.success(b);
    }

    /**
     * 根据主键更新用户表。
     *
     * @param userUpdateRequest 用户表
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PostMapping("/update")
    @Operation(summary = "更新用户")
    public BaseResponse<Boolean> update(@RequestBody UserUpdateRequest userUpdateRequest) {
        Long id = userUpdateRequest.getId();
        if (id == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User user = BeanUtil.copyProperties(userUpdateRequest, User.class);
        boolean b = userService.updateById(user);
        if (!b){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        return ResultUtils.success(true);
    }

    /**
     * 查询所有用户表。
     *
     * @return 所有数据
     */
    @PostMapping("/list")
    @Operation(summary = "查询所有用户")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<User>> list(@RequestBody UserQueryRequest userQueryRequest) {
        QueryWrapper userWrapper = userService.getUserWrapper(userQueryRequest);
        Page<User> userPage = userService.page(new Page<>(userQueryRequest.getCurrent(), userQueryRequest.getPageSize()), userWrapper);
        return ResultUtils.success(userPage);
    }

    /**
     * 根据主键获取用户表。
     *
     * @param id 用户表主键
     * @return 用户表详情
     */
    @GetMapping("/getInfo")
    @Operation(summary = "根据id获取用户")
    public BaseResponse<UserVO> getInfo( Long id) {
        User byId = userService.getById(id);
        UserVO userVO = BeanUtil.copyProperties(byId, UserVO.class);
        return ResultUtils.success(userVO);
    }

}
